package com.freejava.usejpa.controller;

import com.freejava.usejpa.entity.Hero;
import com.freejava.usejpa.services.HeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HeroController {
    @Autowired
    HeroService heroService;

    @GetMapping("/findAllHeros")
    public Page<Hero> findAll() {
        PageRequest pageable = PageRequest.of(2, 3);
        Page<Hero> page = heroService.getHerosByPage(pageable);
        return page;
    }

    @GetMapping("/findHeroesByName")
    public List<Hero> searchHerosByName(@PathVariable String name) {
        List<Hero> heroes = heroService.getHerosByNameStartingWith(name);

        return heroes;
    }

    @GetMapping("/getLatestHero")
    public Hero getLatestHero() {
        return heroService.getLatestHero();
    }


}
